﻿
'importacion de las librerias opentk
Imports OpenTK.Graphics.OpenGL
Imports OpenTK.Platform.Windows

Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.IO

Public Class OGL

#Region "Variables Globales  "

    Private Structure coordenadaGps
        Public x As Double
        Public y As Double

    End Structure

    Dim hRC As System.IntPtr
    Dim hDc As System.IntPtr

    Private coordGps As coordenadaGps
    Private gradoGps As Double
    'variable clase para marcar mapa


    'Public Shared SOURCEFILE As Integer = 0
    'Public Shared SOURCEGPS As Integer = 0
    'Private Const TAG As String = "Touch"
    Public azimut As Double = 20.0R
    Private azimut_rad As Double
    'Public bearing As Double = 0.0R
    'Public bearingCompass As Single = 0.0F
    'Private cadena As String
    'Public cc As CoordinateConversion
    Friend contourArea As Single
    Public contourCont As Integer
    'Public contourLeft(,) As Double = New Double(9999, 1) {}
    'Public contourRight(,) As Double = New Double(9999, 1) {}
    Private cx As Double
    Private cy As Double
    Private cz As Double
    Private dirx As Double
    Private diry As Double
    Public dist As Double = 20.0R
    'flag de control
    Public flagContour As Boolean = True
    Public flagD As Integer = 3
    Public flagMarcar As Boolean = False
    Public flagIncialCoord As Integer = 0
    Public flagMarcarFalse As Boolean
    Public flagInicializacion As Boolean = False
    'Private fps As Single

    Private heightview As Double
    Public mpath As New PathCreated(Me)
    Public msuelo As New Suelo(Me)
    Private mvehicle As New Vehicle(Me)
    Public mapTrayectory As New TrayectoriaMap()
    'Private numframes As Integer = 0
    Private numlinea As Integer = 0
    Private ortox As Double
    Private ortoy As Double
    'angulo de pespectiva
    Public pers As Double = 55.0F
    Public pointOfViewx As Single = 0.0F
    Public pointOfViewy As Single = 0.0F
    Public pointOfViewz As Single = 0.0F
    Public rot As Double = 0.0R
    Public rotRadFixed As Double
    Private rot_rad As Double
    Private upx As Double
    Private upy As Double
    'Private upz As Double
    Private vgps As Double
    Private widthview As Double
    Friend workArea As Double
    Public workwith As Double = My.MySettings.Default.anchoLabor
    Private xgps As Double
    Private xgpsinicial As Double
    Public xleft As Double = 0.0
    Public xleftOld As Double = 0.0
    Public xright As Double = 0.0
    Public xrightOld As Double = 0.0
    Public ygps As Double = 0.0
    Public ygpsinicial As Double = 0.0
    Public yleft As Double = 0.0
    Public yleftOld As Double = 0.0
    Public yright As Double = 0.0
    Public yrightOld As Double = 0.0

#End Region
#Region "inicializaciones  "
    Public Sub Inicializaciones()
        If flagInicializacion = False Then
            GL.ClearColor(255, 255, 255, 0)
         
            widthview = 1
            heightview = 1

            ''Dim arrayOfInt1() As Integer = {10000, 2}
            'Dim contourLeft(,) As Double = New Double(9999, 1) {}
            'Dim contourRight(,) As Double = New Double(9999, 1) {}

            contourCont = 0
            contourArea = 0.0F
            workArea = 0.0F
            xleftOld = 0.0F
            yleftOld = 0.0F
            xrightOld = 0.0F
            yrightOld = 0.0F

            'se pondra la inicializacion del imagenes para poder mostrar
            GL.ShadeModel(ShadingModel.Smooth)
            GL.ClearColor(0.0F, 0.0F, 0.0F, 0.0F)
            GL.ClearDepth(1.0)
            GL.Enable(EnableCap.Texture2D)
            GL.Enable(EnableCap.DepthTest)
            GL.Enable(EnableCap.CullFace)
            GL.CullFace(CullFaceMode.Back)

            GL.DepthFunc(DepthFunction.Lequal)
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest)
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One)
            GL.Enable(EnableCap.Blend)
            flagInicializacion = True
        End If
       
        'glutInit()
    End Sub
#End Region
   
#Region "calcular area"
    Private Function contourAreaCalculator(ByVal paramArrayOfFloat(,) As Double, ByVal paramInt As Integer) As Single
        Dim f As Single = 0.0F
        Dim i As Integer = 0
        Do
            If i >= paramInt - 1 Then
                Return (f + paramArrayOfFloat((paramInt - 1), 0) * paramArrayOfFloat(0, 1) - paramArrayOfFloat((paramInt - 1), 1) * paramArrayOfFloat(0, 0)) / 2.0F / 10000.0F
            End If
            f = f + paramArrayOfFloat(i, 0) * paramArrayOfFloat((i + 1), 1) - paramArrayOfFloat(i, 1) * paramArrayOfFloat((i + 1), 0)
            i += 1
        Loop
    End Function
#End Region

#Region "camara"

    Public Sub cameraWord()
        GL.MatrixMode(MatrixMode.Projection)
        GL.LoadIdentity()
        Dim perspective As OpenTK.Matrix4 = OpenTK.Matrix4.CreatePerspectiveFieldOfView(((pers * System.Math.PI) / 180), 1.0F * widthview / heightview, 0.1F, 1000.0F)
        GL.MultMatrix(perspective)

        'Perspective(pers, 1.0F * widthview / heightview, 0.1F, 1000.0F)
        GL.MatrixMode(MatrixMode.Modelview)

        azimut_rad = (3.1415926535897931R * azimut / 180.0R)
        rot_rad = (3.1415926535897931R * rot / 180.0R)
        cx = (dist * Math.Cos(azimut_rad) * Math.Sin(rot_rad) + xgps)
        cy = (-dist * Math.Cos(azimut_rad) * Math.Cos(rot_rad) + ygps)
        cz = (dist * Math.Sin(azimut_rad))
        upx = (-Math.Sin(rot_rad))
        upy = Math.Cos(rot_rad)
        'upz = 0.0R
        'If Not flagPanning Then
        pointOfViewx = (xgps)
        pointOfViewy = (ygps)
        pointOfViewz = 0.0F
        '    gluLookAt(cx, cy, cz, xgps, ygps, 0.0F, 0.0F, 0.0F, 1.0F)
        rotRadFixed = rot_rad
        'End If



        cx = (dist * Math.Cos(azimut_rad) * Math.Sin(rotRadFixed) + pointOfViewx)
        cy = (-dist * Math.Cos(azimut_rad) * Math.Cos(rotRadFixed) + pointOfViewy)
        cz = (dist * Math.Sin(azimut_rad))
        Dim lookat As OpenTK.Matrix4 = OpenTK.Matrix4.LookAt(cx, cy, cz, pointOfViewx, pointOfViewy, pointOfViewz, 0.0F, 0.0F, 1.0F)
        GL.MatrixMode(MatrixMode.Modelview)
        GL.LoadMatrix(lookat)
        'LookAt(cx, cy, cz, pointOfViewx, pointOfViewy, pointOfViewz, 0.0F, 0.0F, 1.0F)


    End Sub
#End Region
#Region "render"
    Public Sub BucleOpenGl()
        Render()
    End Sub
    Private Sub Render()
        Do
            GL.Clear(ClearBufferMask.ColorBufferBit Or ClearBufferMask.DepthBufferBit)
            GL.LoadIdentity()
            'traigo las coordenadas 
            If MainForm.conectado Then
                'If MainForm.flagPausa = 0 Then
                xgps = coordGps.x
                ygps = coordGps.y
                rot = gradoGps
                vgps = 30
                vgps = 1.8520043D * vgps


                'End If
                'me ubico en el mapa de rendmiento
                mapTrayectory.drawMap(New DotSpatial.Topology.Coordinate(coordGps.x, coordGps.y))
            End If

            If flagIncialCoord = 0 And (Not xgps = 0 Or Not ygps = 0) Then
                xgpsinicial = xgps
                ygpsinicial = ygps
                flagIncialCoord = 1
                rot = gradoGps
            End If
            xgps -= xgpsinicial
            ygps -= ygpsinicial
            xgps = xgps * 100000
            ygps = ygps * 100000
            vgps = (1.8520043R * vgps)
            rot = (-1.0R * gradoGps)

            'GL.Clear(ClearBufferMask.ColorBufferBit)
            'GL.Clear(ClearBufferMask.DepthBufferBit)
            'GL.LoadIdentity()

            cameraWord()

            msuelo.draw()
            dirx = (-Math.Sin(3.1415926535897931R * rot / 180.0R))
            diry = Math.Cos(3.1415926535897931R * rot / 180.0R)
            ortox = (-diry)
            ortoy = dirx
            xright = (xgps - ortox * workwith / 2.0R)
            yright = (ygps - ortoy * workwith / 2.0R)
            xleft = (xgps + ortox * workwith / 2.0R)
            yleft = (ygps + ortoy * workwith / 2.0R)


            If flagMarcar Then
                Dim arrayOfFloat(,) As Double = New Double(3, 1) {}
                arrayOfFloat(0, 0) = xleftOld
                arrayOfFloat(0, 1) = yleftOld
                arrayOfFloat(1, 0) = xrightOld
                arrayOfFloat(1, 1) = yrightOld

                arrayOfFloat(2, 0) = xright
                arrayOfFloat(2, 1) = yright
                arrayOfFloat(3, 0) = xleft
                arrayOfFloat(3, 1) = yleft


                'workArea += contourAreaCalculator(arrayOfFloat, 4)
                xleftOld = (xleft)
                yleftOld = (yleft)
                xrightOld = (xright)
                yrightOld = (yright)


                If flagContour Then

                    mpath.addPoint(xleft, yleft, 0.1F, xright, yright, 0.1F, 0.0F, 0.0F, 65536.0F, 65536.0F / 2.0F)
                

                End If
                'MainForm.hetrariaTrabajadas.Text = "he " & workArea & " km/ht"
                
            End If

            'GL.LoadIdentity()
            'cameraWord()
            mpath.draw()
            'GL.LoadIdentity()
            cameraWord()

            GL.Translate(xgps, ygps, 0.0F)
            GL.Rotate(rot, 0.0F, 0.0F, 1.0F)

            mvehicle.draw()
            numlinea = (1 + numlinea)

            Application.DoEvents()
            'SwapBuffers(hDc)
            If (MainForm.sw.IsRunning) Then
                'MainForm.GlControl1.Context.SwapBuffers()
                MainForm.GlControl1.SwapBuffers()

            Else

                EliminarOpenGL()
                Exit Do
            End If
        Loop

    End Sub

    Public Sub EliminarOpenGL()

        'wglMakeCurrent(IntPtr.Zero, IntPtr.Zero)
        'wglDeleteContext(hRC)
        MainForm.GlControl1.Dispose()
        If MainForm.sw.IsRunning Then
            MainForm.sw.Stop()
        End If
    End Sub
  
#End Region
    Public Sub Cordenadas(ByVal X As Double, ByVal Y As Double, ByVal grado As Double)
        coordGps.x = X
        coordGps.y = Y
        gradoGps = grado
    End Sub

End Class

